package com.monise.view;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;

import com.monise.dao.DAO.ApontamentoHoraDAO;
import com.monise.model.ApontamentoHora;
import com.monise.model.Funcionario;
import com.monise.util.Util;


public class FrameApontarHoras extends JInternalFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField txtVoltaAlmoco;
	private JTextField txtSaida;
	private JTextField txtEntrada;
	private JTextField txtSaidaAlmoco;
	private JLabel lblHora;
	private JLabel lblCodigo;
	private JLabel lblNome;
	private JButton btnLimpar;	
	private JButton btnSalvar;
	private JLabel lblData;
	
	private Funcionario funcionario;
	private Util util = new Util();
	private JLabel lblCdigo;
	private JLabel lblNome_1;
	private JTextArea txtObservacao;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FrameApontarHoras frame = new FrameApontarHoras(FrameSistema.getFuncionario());
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public FrameApontarHoras(Funcionario funcionario) {
		
		this.funcionario = funcionario;
			
		setTitle("Apontamento de Horas");
		setClosable(true);
		setBounds(100, 100, 520, 493);
		
		lblHora = new JLabel("New label");
		lblHora.setHorizontalAlignment(SwingConstants.CENTER);
		lblHora.setFont(new Font("Tahoma", Font.BOLD, 20));
		lblHora.setBackground(Color.YELLOW);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Hor\u00E1rio", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
	    lblCodigo = new JLabel("New label");
	    lblCodigo.setHorizontalAlignment(SwingConstants.LEFT);
		
		lblNome = new JLabel("New label");
		
		btnLimpar = new JButton("Limpar");
		btnLimpar.setIcon(new ImageIcon(FrameApontarHoras.class.getResource("/com/monise/imagens/Trash.png")));
		
		btnSalvar = new JButton("Salvar");
		btnSalvar.setIcon(new ImageIcon(FrameApontarHoras.class.getResource("/com/monise/imagens/Save.png")));
		
		JLabel lbl = new JLabel("Data:");
		lblData = new JLabel("New label");
		
		lblCdigo = new JLabel("C\u00F3digo:");
		lblCdigo.setFont(new Font("Tahoma", Font.BOLD, 11));
		
		lblNome_1 = new JLabel("Nome:");
		lblNome_1.setFont(new Font("Tahoma", Font.BOLD, 11));
		
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(133)
							.addComponent(lblHora, GroupLayout.PREFERRED_SIZE, 251, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(22)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblCdigo)
								.addComponent(lbl))
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
									.addGap(28)
									.addComponent(lblData))
								.addGroup(groupLayout.createSequentialGroup()
									.addGap(18)
									.addComponent(lblCodigo)))
							.addGap(129)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(btnSalvar)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(btnLimpar))
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblNome_1)
									.addGap(18)
									.addComponent(lblNome, GroupLayout.PREFERRED_SIZE, 225, GroupLayout.PREFERRED_SIZE))))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(61)
							.addComponent(panel, GroupLayout.PREFERRED_SIZE, 390, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(20)
					.addComponent(lblHora, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 227, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblCdigo)
						.addComponent(lblNome_1)
						.addComponent(lblNome)
						.addComponent(lblCodigo))
					.addGap(32)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblData)
						.addComponent(lbl)
						.addComponent(btnSalvar)
						.addComponent(btnLimpar))
					.addGap(32))
		);
		
		JLabel lblEntrada = new JLabel("Volta almo\u00E7o:");
		
		JLabel lblNewLabel_1 = new JLabel("Sa\u00EDda:");
		
		txtVoltaAlmoco = new JFormattedTextField((util.setMascara("##:##")));
		txtVoltaAlmoco.setColumns(10);
		
		txtSaida = new JFormattedTextField((util.setMascara("##:##")));
		txtSaida.setColumns(10);
		
		JLabel lblObservao = new JLabel("Observa\u00E7\u00E3o:");
		
		txtObservacao = new JTextArea();
		
		JLabel label = new JLabel("Entrada:");
		
		txtEntrada = new JFormattedTextField((util.setMascara("##:##")));
		txtEntrada.setColumns(10);
		
		JLabel lblSadaAlmoo = new JLabel("Sa\u00EDda almo\u00E7o:");
		
		txtSaidaAlmoco = new JFormattedTextField((util.setMascara("##:##")));
		txtSaidaAlmoco.setColumns(10);
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(18)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(label, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(txtEntrada, GroupLayout.PREFERRED_SIZE, 61, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel.createSequentialGroup()
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addComponent(lblObservao)
								.addComponent(lblEntrada))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addComponent(txtObservacao, GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
								.addGroup(gl_panel.createSequentialGroup()
									.addComponent(txtVoltaAlmoco, GroupLayout.PREFERRED_SIZE, 61, GroupLayout.PREFERRED_SIZE)
									.addGap(18)
									.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
										.addComponent(lblSadaAlmoo)
										.addGroup(gl_panel.createSequentialGroup()
											.addComponent(lblNewLabel_1)
											.addPreferredGap(ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
											.addComponent(txtSaida, GroupLayout.PREFERRED_SIZE, 53, GroupLayout.PREFERRED_SIZE)))))))
					.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addComponent(txtSaidaAlmoco, GroupLayout.PREFERRED_SIZE, 53, GroupLayout.PREFERRED_SIZE)
					.addGap(44))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap(37, Short.MAX_VALUE)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(label)
						.addComponent(txtEntrada, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(txtSaidaAlmoco, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblSadaAlmoo))
					.addGap(36)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblEntrada)
						.addComponent(lblNewLabel_1)
						.addComponent(txtSaida, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(txtVoltaAlmoco, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(27)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addComponent(lblObservao)
						.addComponent(txtObservacao, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE))
					.addGap(22))
		);
		panel.setLayout(gl_panel);
		getContentPane().setLayout(groupLayout);
		
		getHora();
		getData();
		preencherDadosFuncionario();
		
		btnSalvar.addActionListener(new Salvar());
		btnLimpar.addActionListener(new Limpar());

	}
	
	private void getHora() {  
	    StringBuilder sb = new StringBuilder();  
	    GregorianCalendar d = new GregorianCalendar();  
	      
	    sb.append( d.get( GregorianCalendar.HOUR_OF_DAY ) );  
	    sb.append( ":" );  
	    sb.append( d.get( GregorianCalendar.MINUTE ) );  

	    lblHora.setText(sb.toString());
	}  
	
	private void preencherDadosFuncionario(){
		lblCodigo.setText(funcionario.getPkFuncionario().toString());
		lblNome.setText(funcionario.getNome());
	}
	
	private void getData(){
		String data = (new java.text.SimpleDateFormat("dd/MM/yyyy").format(new java.util.Date(System.currentTimeMillis())));
		lblData.setText(data);
	}
	
	private void salvarDados(){
		
		ApontamentoHora apontamento = new ApontamentoHora();

		DateFormat forma = new SimpleDateFormat("dd/MM/yyyy");    
	    java.util.Date data = null;
		try {
			data = new java.util.Date(forma.parse(lblData.getText()).getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}    

		 apontamento.setData(data);
		 apontamento.setFuncionario(funcionario);
		 apontamento.setHoraEntrada(txtEntrada.getText());
		 apontamento.setHoraSaida(txtSaida.getText());
		 apontamento.setHoraAlmocoS(txtSaidaAlmoco.getText());
		 apontamento.setHoraAlmocoV(txtVoltaAlmoco.getText());
		 apontamento.setObservacao(txtObservacao.getText());
		 
		 try{
			 ApontamentoHoraDAO apontamentoDao = new ApontamentoHoraDAO();
			 apontamentoDao.save(apontamento);
			 JOptionPane.showMessageDialog(null, "Apontamento de horas salvo com sucesso!");
		 }catch (Exception e) {
			 JOptionPane.showMessageDialog(null, "Ocorreu um erro ao salvar o apontamento de horas!");
			 e.printStackTrace();
		}
	}
	
	private void limparCampos(){
		txtVoltaAlmoco.setText("");
		txtSaida.setText("");
		txtObservacao.setText("");
		txtEntrada.setText("");
		txtSaidaAlmoco.setText("");
	}
	
	private class Salvar implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			salvarDados();
		}
	}
	
	private class Limpar implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			limparCampos();
		}
	}
}
